package com.soft.manage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.soft.common.exception.ServiceException;
import com.soft.common.utils.DateUtils;
import com.soft.common.utils.SecurityUtils;
import com.soft.common.utils.emil.IEmailService;
import com.soft.constants.ruanzhuManageConstant;
import com.soft.manage.domain.CustomUser;
import com.soft.manage.domain.dto.smsMessageDto;
import com.soft.manage.mapper.CustomUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.soft.manage.mapper.RuanzhuApplyInfoMapper;
import com.soft.manage.domain.RuanzhuApplyInfo;
import com.soft.manage.service.IRuanzhuApplyInfoService;

import javax.annotation.Resource;

/**
 * 软著申请情况Service业务层处理
 *
 * @author mobin
 * @date 2025-06-04
 */
@Service
public class RuanzhuApplyInfoServiceImpl extends ServiceImpl<RuanzhuApplyInfoMapper, RuanzhuApplyInfo> implements IRuanzhuApplyInfoService {
    @Autowired
    private RuanzhuApplyInfoMapper ruanzhuApplyInfoMapper;
    @Resource
    private CustomUserMapper customUserMapper;
    @Resource
    private IEmailService emailService;

    /**
     * 查询软著申请情况
     *
     * @param id 软著申请情况主键
     * @return 软著申请情况
     */
    @Override
    public RuanzhuApplyInfo selectRuanzhuApplyInfoById(Long id) {
        return ruanzhuApplyInfoMapper.selectRuanzhuApplyInfoById(id);
    }

    /**
     * 查询软著申请情况列表
     *
     * @param ruanzhuApplyInfo 软著申请情况
     * @return 软著申请情况
     */
    @Override
    public List<RuanzhuApplyInfo> selectRuanzhuApplyInfoList(RuanzhuApplyInfo ruanzhuApplyInfo) {
        return ruanzhuApplyInfoMapper.selectRuanzhuApplyInfoList(ruanzhuApplyInfo);
    }

    /**
     * 新增软著申请情况
     *
     * @param ruanzhuApplyInfo 软著申请情况
     * @return 结果
     */
    @Override
    public int insertRuanzhuApplyInfo(RuanzhuApplyInfo ruanzhuApplyInfo) {
        ruanzhuApplyInfo.setCreateTime(DateUtils.getNowDate());
        ruanzhuApplyInfo.setCreateBy(SecurityUtils.getUsername());
        return ruanzhuApplyInfoMapper.insertRuanzhuApplyInfo(ruanzhuApplyInfo);
    }

    /**
     * 修改软著申请情况
     *
     * @param ruanzhuApplyInfo 软著申请情况
     * @return 结果
     */
    @Override
    public int updateRuanzhuApplyInfo(RuanzhuApplyInfo ruanzhuApplyInfo) {
        ruanzhuApplyInfo.setUpdateTime(DateUtils.getNowDate());
        ruanzhuApplyInfo.setUpdateBy(SecurityUtils.getUsername());
        return ruanzhuApplyInfoMapper.updateRuanzhuApplyInfo(ruanzhuApplyInfo);
    }

    /**
     * 批量删除软著申请情况
     *
     * @param ids 需要删除的软著申请情况主键
     * @return 结果
     */
    @Override
    public int deleteRuanzhuApplyInfoByIds(Long[] ids) {
        return ruanzhuApplyInfoMapper.deleteRuanzhuApplyInfoByIds(ids);
    }

    /**
     * 删除软著申请情况信息
     *
     * @param id 软著申请情况主键
     * @return 结果
     */
    @Override
    public int deleteRuanzhuApplyInfoById(Long id) {
        return ruanzhuApplyInfoMapper.deleteRuanzhuApplyInfoById(id);
    }

    /**
     * 发送短信
     * @param smsMessageDto
     * @return
     */
    @Override
    public boolean sendMessage(smsMessageDto smsMessageDto) {
        if(StrUtil.isEmpty(smsMessageDto.getMessage())){
            throw new ServiceException("发送消息不能为空");
        }
        if(StrUtil.isEmpty(smsMessageDto.getMessageType())){
            throw new ServiceException("消息类型不能为空");
        }
        if(ObjectUtil.isNull(smsMessageDto.getApplyUser())){
            throw new ServiceException("applyUser不能为空");
        }
        String userName = smsMessageDto.getUserName();
        String sendAddr = smsMessageDto.getSendAddr();
        boolean isPhoneSend = StrUtil.equals(smsMessageDto.getMessageType(), ruanzhuManageConstant.SEND_MESSAGE_TYPE_PHONE);
        if(StrUtil.isEmpty(smsMessageDto.getUserName()) || StrUtil.isEmpty(smsMessageDto.getSendAddr())){
            CustomUser customUser = customUserMapper.selectCustomUserById(smsMessageDto.getApplyUser());
            userName = customUser.getCustomName();
            sendAddr = isPhoneSend ? customUser.getPhone() : customUser.getEmail();
        }
        if(isPhoneSend){
            // 手机发送

        }else{
            // 邮箱发送
            emailService.sendHtml("1940787338@qq.com", sendAddr, "睿码创谷--软著申请", smsMessageDto.getMessage());
        }
        return true;
    }
}
